﻿using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using MyExeSetting;
using MySQLite.DataHelp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyExe.Help
{
    /// <summary>
    /// 
    /// </summary>
    public class HttpServerHelper
    {
        public static void Start()
        {
            Task.Run(() =>
            {
                Excute();
            });
        }

        private static void Excute()
        {
            try
            {
                LogHelper.AddLocalLogInfo("启动服务");
                string url = MyExeSettingHelper.Instance.LocalServerUrl;
                string webPath = MyExeSettingHelper.Instance.Directories;
                IWebHost _host = new WebHostBuilder()
                  .UseKestrel()
                  .UseUrls(url)
                  .UseWebRoot(webPath)
                  .UseStartup<Startup>()
                  .Build();
                _host.Run();
            }
            catch (Exception ex)
            {
                LogHelper.AddLocalLogError($"启动服务失败,{ex.Message}");
            }
        }

    }

    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            //跨域设置
            //允许所有跨域
            services.AddCors(options => options.AddPolicy("CorsPolicy",
                 builder =>
                 {
                     builder.AllowAnyMethod()
                         .SetIsOriginAllowed(_ => true)
                         .AllowAnyHeader()
                         .AllowCredentials();
                 }));
            services.AddMvc();
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Latest);
            services.AddMvcCore();
        }


        public void Configure(IApplicationBuilder app)
        {
            //允许所有跨域
            app.UseCors("CorsPolicy");

            app.UseStaticFiles();

            string listpath = MyExeSettingHelper.Instance.Directories;
            if (listpath != "")
            {
                string[] patharray = listpath.Split(",");
                foreach (string path in patharray)
                {
                    string virturePath = path.Replace(":", "").Replace("\\", "");
                    string storePath = path;
                    if (!Directory.Exists(storePath))
                    {
                        Directory.CreateDirectory(storePath);
                    }
                    //app.UseDirectoryBrowser($"/{virturePath}");
                    app.UseStaticFiles(new StaticFileOptions
                    {
                        FileProvider = new PhysicalFileProvider(storePath),
                        RequestPath = $"/{virturePath}",
                        ServeUnknownFileTypes = true,
                        //ContentTypeProvider = fileProvider,//设置文件提供器
                    });
                }
            }

            app.UseMvc(s => {
                s.MapRoute("default", "{action}/{id?}", "Play");
            });
        }
    }

}
